package generadorDescriptores.forms;

import generadorDescriptores.Fachada;
import generadorDescriptores.database.ConnectionManager;
import generadorDescriptores.forms.edition.FormNuevaConexionDialog;

import java.awt.Window;
import java.util.ArrayList;

import javax.swing.JDialog;
import javax.swing.JFrame;

public class AdminConnections extends javax.swing.JDialog {

	Fachada fachada = Fachada.getInstance();

	/** Creates new form AdminConnections */
	public AdminConnections(JFrame parent, boolean modal) {
		super(parent, modal);
		initComponents();
		this.startUp();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		listConexiones = new java.awt.List();
		jLabel1 = new javax.swing.JLabel();
		ButtonNueva = new javax.swing.JButton();
		ButtonEliminar = new javax.swing.JButton();
		ButtonAceptar = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		addWindowFocusListener(new java.awt.event.WindowFocusListener() {
			public void windowGainedFocus(java.awt.event.WindowEvent evt) {
				formWindowGainedFocus(evt);
			}

			public void windowLostFocus(java.awt.event.WindowEvent evt) {
			}
		});

		jLabel1.setText("Conexiones: ");

		ButtonNueva.setText("Nueva Conexi\u00f3n");
		ButtonNueva.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ButtonNuevaActionPerformed(evt);
			}
		});

		ButtonEliminar.setText("Eliminar Conexi\u00f3n");
		ButtonEliminar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ButtonEliminarActionPerformed(evt);
			}
		});

		ButtonAceptar.setText("Aceptar");
		ButtonAceptar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ButtonAceptarActionPerformed(evt);
			}
		});

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								org.jdesktop.layout.GroupLayout.TRAILING,
								layout
										.createSequentialGroup()
										.add(23, 23, 23)
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.TRAILING,
																false)
														.add(
																org.jdesktop.layout.GroupLayout.LEADING,
																layout
																		.createSequentialGroup()
																		.add(
																				ButtonNueva)
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.UNRELATED)
																		.add(
																				ButtonEliminar)
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)
																		.add(
																				ButtonAceptar,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																				94,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
														.add(
																org.jdesktop.layout.GroupLayout.LEADING,
																jLabel1)
														.add(
																org.jdesktop.layout.GroupLayout.LEADING,
																listConexiones,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))
										.addContainerGap(41, Short.MAX_VALUE)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.add(18, 18, 18)
										.add(jLabel1)
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												listConexiones,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(32, 32, 32)
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(ButtonNueva).add(
																ButtonEliminar)
														.add(ButtonAceptar))
										.addContainerGap(21, Short.MAX_VALUE)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void ButtonAceptarActionPerformed(java.awt.event.ActionEvent evt) {
		this.dispose();
	}

	private void startUp() {
		this.setTitle("Administrar Conexiones");
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.loadConnections();
	}

	private void loadConnections() {
		this.listConexiones.removeAll();
		ArrayList<ConnectionManager> connections = fachada
				.getActiveConnections();
		for (ConnectionManager conexion : connections) {
			listConexiones.add(conexion.toString());
		}
	}

	private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {
		this.loadConnections();
	}

	private void ButtonNuevaActionPerformed(java.awt.event.ActionEvent evt) {
		FormNuevaConexionDialog.main(null);
	}

	private void ButtonEliminarActionPerformed(java.awt.event.ActionEvent evt) {
		ArrayList<ConnectionManager> connections = fachada
				.getActiveConnections();
		try {
			for (ConnectionManager conexion : connections) {
				if (listConexiones.getSelectedItem().toString().equals(
						conexion.toString())) {
					fachada.removeConnection(conexion);
				}
			}
		} catch (Exception e) {
			listConexiones.removeAll();
			loadConnections();
		}
		this.loadConnections();
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				AdminConnections dialog = new AdminConnections(new JFrame(),
						true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					public void windowClosing(java.awt.event.WindowEvent e) {

					}
				});
				dialog.setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton ButtonAceptar;
	private javax.swing.JButton ButtonEliminar;
	private javax.swing.JButton ButtonNueva;
	private javax.swing.JLabel jLabel1;
	private java.awt.List listConexiones;
	// End of variables declaration//GEN-END:variables

}